Ext.onReady(function(){
    var ordersrow;
    var ticketsrow;
    var current_order_id; //当前选择的order的id
    var current_ticket_id; //当前选择的ticket的id
    var current_order_status; //当前order的状态
    var current_ticket_status; //当前ticket的状态
    setTimeout("refreshDS()", 300000);
    refreshDS = function(){
        var time = parseInt(refreshTime.getValue()) * 60000;
        orderds.reload();
        ticketds.baseParams.id = "";
        ticketds.reload();
        setTimeout("refreshDS()", time);
    };
    var before = new Ext.form.Checkbox({
        fieldLabel: '以前的',
        hideLabel: true,
        boxLabel: '以前的',
        name: 'before',
        checked: false,
        anchor: '90%'
    });
    var created_d = new Ext.form.Radio({
        fieldLabel: '',
        hideLabel: true,
        name: 'date_type',
        boxLabel: '订单日期',
        checked: true,
        inputValue: 'created_at'
    });
    var fly_d = new Ext.form.Radio({
        fieldLabel: '',
        hideLabel: true,
        name: 'date_type',
        boxLabel: '乘机日期',
        checked: false,
        inputValue: ''
    });
    var date_field = new Ext.form.DateField({
        fieldLabel: '日期查询',
        name: 'date',
        altFormats: 'Y-m-d',
        format: 'Y-m-d', //格式化日期
        //disabledDays: [0,6],	//不让选择周六，周日
        //disabledDaysText: "周末要休息",
        invalidText: "您输入的日期格式不对，正确格式为2008-01-01",
        allowBlank: false,
        //emptyText: '请选择...',
        inputType: '2008-01-01',
        //anchor: '50%'
        width: 100,
        value: today
    });
    var refreshTime = new Ext.form.ComboBox({
        store: new Ext.data.Store({
            reader: new Ext.data.ArrayReader({}, ['value']),
            data: [['1'], ['5'], ['10'], ['30']]
        }),
        name: 're_time',
        fieldLabel: '刷新间隔(分)',
        resiable: true,
        typeAhead: true,
        width: 60,
        handleHeight: 100,
        displayField: 'value',
        valueField: 'value',
        nameField: 're_time',
        selectOnFocus: true,
        mode: 'local',
        selectOnFocus: true,
        triggerAction: 'all', //这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
        readOnly: true,
        allowBlank: true,
        value: '5'
    });
    
    //提交按钮
    var submitButton = new Ext.Button({
        text: '查询',
        handler: function(){
            if (searchFormPanel.form.isValid()) {
                var date = date_field.getValue().format('Y-m-d');
                var date_type = "";
                if (created_d.checked) {
                    date_type = "created_at";
                }
                else if (fly_d.checked) {
                    date_type = "fly_date";
                }
                if (before.checked) {
                    orderds.baseParams.before = "yes";
                }
                else {
                    orderds.baseParams.before = "no";
                }
                orderds.baseParams.date_type = date_type;
                orderds.baseParams.date = date;
                orderds.reload();
                ticketds.baseParams.id = "";
                ticketds.reload();
            }
        }
    })
    
    var searchFormPanel = new Ext.form.FormPanel({
        //baseCls: 'x-plain',
        labelWidth: 75,
        width: '100%',
        frame: true,
        title: '出票',
        labelAlign: 'right',
        items: [{
            layout: 'column',
            border: false,
            items: [{
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [date_field]
            }, {
                columnWidth: .08,
                layout: 'form',
                border: false,
                items: [created_d]
            }, {
                columnWidth: .1,
                layout: 'form',
                border: false,
                items: [fly_d]
            }, {
                columnWidth: .2,
                layout: 'form',
                border: false,
                items: [refreshTime]
            }, {
                columnWidth: .1,
                labelWidth: 0,
                border: false,
                items: [before]
            }, {
                columnWidth: .25,
                layout: 'form',
                border: false,
                items: [submitButton]
            }]
        }]
    });
    
    searchFormPanel.render('out_ticket_form');
    
    //############## 订单Grid ##############
    var ordercm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        hidden: true
    }, {
        header: '订单编号',
        dataIndex: 'order_no',
        sortable: true,
        width: 115
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 60
    }, {
        header: '客户名称',
        dataIndex: 'customer',
        width: 60,
        hidden: true
    }, {
        header: '订单状态',
        dataIndex: 'status',
        sortable: true,
        width: 60
    }, {
        header: '总金额',
        dataIndex: 'price_sum',
        sortable: true,
        width: 50
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        sortable: true,
        width: 70
    }, {
        header: '起飞(T)',
        dataIndex: 'fly_time',
        sortable: true,
        width: 50
    }, {
        header: '送票地址',
        dataIndex: 'send_address',
        sortable: true,
        width: 130
    }, {
        header: '联系人',
        dataIndex: 'link_man',
        sortable: true,
        width: 50,
        hidden: true
    }, {
        header: '联系电话',
        dataIndex: 'link_tel',
        width: 90,
        hidden: true
    }, {
        header: '预定员',
        dataIndex: 'creator',
        sortable: true,
        width: 50
    }, {
        header: '预定(T)',
        dataIndex: 'created_at',
        width: 110,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
        sortable: true
    }, {
        header: '出票员',
        dataIndex: 'ticket_outer',
        sortable: true,
        width: 50
    }, {
        header: '出票(T)',
        dataIndex: 'ticket_out_date',
        width: 110,
        sortable: true,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i')
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        sortable: true,
        width: 60
    }, {
        header: '客户要求',
        dataIndex: 'order_request',
        width: 120
    
    }]);
    
    var orderds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getOutTicketList'
        }),
        reader: orderReader
    });
    var ordergridpanel = new Ext.grid.GridPanel({
        el: 'ordersgrid',
        ds: orderds,
        cm: ordercm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 330,
        //frame: true,
        title: '订单信息'
    });
    ordergridpanel.render();
    orderds.load();
    
    //双击订单查看票详细
    ordergridpanel.on('rowdblclick', function(ordergridpanel, rowIndex, e){
        var current_order_selected = orderds.data.items[rowIndex].id;
        ordersrow = orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        ticketds.baseParams.id = current_order_id;
        ticketds.reload();
    });
    //订单右键菜单
    ordergridpanel.on('rowcontextmenu', function(customergridpanel, rowIndex, e){
        var current_order_selected = orderds.data.items[rowIndex].id;
        ordersrow = orderds.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        e.preventDefault();
        rightClick.showAt(e.getXY());
    });
    
    //############ 订单-右键菜单 ##########
    var rightClick = new Ext.menu.Menu({
        items: [{
            handler: rightMenu2Fn,
            text: '【确认出票】'
        }, {
            handler: rightMenu5Fn,
            text: '【手工出票】'
        }, "-", {
            handler: rightMenu4Fn,
            text: '【提取PNR】'
        }, {
            handler: rightMenu1Fn,
            text: '【修改订单】'
        }, {
            handler: rightMenu3Fn,
            text: '【取消订单】'
        }]
    });
    function rightMenu1Fn(){
        ModOrder(orderds, current_order_id, ticketds);
    }
    function rightMenu2Fn(){
        if (current_order_status == ORDER_STATUS_YDD) {
            getTicketNo(current_order_id, ticketds, orderds);
        }
        else {
            Ext.MessageBox.alert('操作提示', '只有预订单才可以出票');
        }
    }
    function rightMenu3Fn(){
        var current_order_no = ordersrow.get("order_no");
        cancelOrder(current_order_status, current_order_id, current_order_no, orderds, ticketds);
    }
    function rightMenu4Fn(){
        showPnrInfo(current_order_id);
    }
    function rightMenu5Fn(){
        Ext.Ajax.request({
            url: '/orders/outTicketByOrder',
            params: 'orderid=' + current_order_id + '&handwork=yes',
            method: 'GET',
            success: function(result, request){
                if (result.responseText.indexOf("成功") >= 0) {
                    Ext.MessageBox.alert('操作成功', result.responseText);
                    ticketds.baseParams.id = "";
                    ticketds.reload();
                    orderds.reload();
                }
                else {
                    Ext.MessageBox.alert('操作失败', result.responseText);
                }
            },
            failure: function(result, request){
                Ext.MessageBox.alert('操作失败', '没有发现操作方法!');
            }
        });
    }
    
    //### 机票Grid ###
    var ticketcm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        hidden: true
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 60
    }, {
        header: '旅客姓名',
        dataIndex: 'passenger_name',
        width: 60
    }, {
        header: '证件号码',
        dataIndex: 'card',
        width: 140
    }, {
        header: '票号',
        dataIndex: 'ticket_no',
        width: 110
    }, {
        header: '状态',
        dataIndex: 'status_type',
        width: 50
    }, {
        header: '出票状态',
        dataIndex: 'status',
        sortable: true,
        width: 60
    }, {
        header: '航班号',
        dataIndex: 'air_no',
        width: 60
    }, {
        header: '航程',
        dataIndex: 'fromto',
        width: 60
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 60
    }, {
        header: '舱位',
        dataIndex: 'position',
        width: 40
    }, {
        header: '机票价格',
        dataIndex: 'price',
        width: 60
    }, {
        header: '机场费税',
        dataIndex: 'airdrome_tax',
        width: 60
    }, {
        header: '附加费',
        dataIndex: 'fuel_tax',
        width: 60
    }, {
        header: 'F',
        dataIndex: 'price_F',
        width: 40
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 60
    }, {
        header: '保险F',
        dataIndex: 'insurance_F',
        width: 50
    }, {
        header: '其他',
        dataIndex: 'other',
        width: 50
    }, {
        header: '积分',
        dataIndex: 'point',
        width: 50
    }, {
        header: '合计',
        dataIndex: 'total_price',
        width: 60
    
    }]);
    ticketcm.defaultSortable = true;
    var ticketds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getTicketsByOrderID?showCancelTicket=no'
        }),
        reader: ticketReader
    });
    
    var ticketgridpanel = new Ext.grid.GridPanel({
        el: 'ticketsgrid',
        ds: ticketds,
        cm: ticketcm,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: false,
        height: 180,
        //frame: true,
        title: '航班信息'
    });
    
    ticketgridpanel.render();
    ticketds.load();
    
    //############ 票-右键菜单 ##########
    function ticketRightMenu0(){
        ModTicketNo(ticketds, current_ticket_id);
    }
    function ticketRightMenu1(){
        ModTicket(ticketds, current_ticket_id, orderds);
    }
    function ticketRightMenu2(){
        var current_passenger_name = ticketsrow.get("passenger_name");
        cancelTicket(current_ticket_status, current_ticket_id, current_passenger_name, ticketds);
    }
    var ticketRightClick = new Ext.menu.Menu({
        items: [{
            handler: ticketRightMenu0,
            text: '【维护票号】'
        }, "-", {
            handler: ticketRightMenu1,
            text: '【编辑修改】'
        }, {
            handler: ticketRightMenu2,
            text: '【取消乘客】'
        }]
    });
    ticketgridpanel.on('rowcontextmenu', function(ticketgridpanel, rowIndex, e){
        var current_ticket_selected = ticketds.data.items[rowIndex].id;
        ticketsrow = ticketds.getById(current_ticket_selected);//ds为绑定到grid的数据源
        current_ticket_id = ticketsrow.get("id");
        current_ticket_status = ticketsrow.get("status");
        e.preventDefault();
        ticketRightClick.showAt(e.getXY());
    });
    //维护票号
    function ModTicketNo(dataStore, id){
        var ticketNo = new Ext.form.TextField({
            fieldLabel: '票号',
            name: 'ticket_no',
            allowBlank: false,
            blankText: '票号必须填写',
            anchor: '90%'
        });
        var ticketNoFormPanel = new Ext.form.FormPanel({
            //baseCls: 'x-plain',
            labelWidth: 60,
            labelAlign: 'right',
            frame: true,
            reader: new Ext.data.JsonReader({
                root: 'root'
            }, ['ticket_no']),
            url: "/orders/saveTicketNo?id=" + id,
            items: [ticketNo]
        });
        ticketNoFormPanel.form.load({
            url: '/orders/getEditTicketInfoByID?id=' + id,
            waitMsg: '正在载入数据...',
            success: function(form, action){
            //Ext.MessageBox.alert('提示信息', '信息加载成功！');
            },
            failure: function(form, action){
                Ext.MessageBox.alert('提示信息', '信息加载失败');
            }
        });
        var ticketNoWindow = new Ext.Window({
            title: '保存票号',
            width: 260,
            height: 130,
            layout: 'fit',
            plain: true,
            bodyStyle: 'padding:5px;',
            buttonAlign: 'center',
            items: ticketNoFormPanel,
            buttons: [{
                text: '保存',
                handler: function(){
                    if (ticketNoFormPanel.form.isValid()) {
                        var go = false;
                        var t_no = ticketNo.getValue();
                        if (t_no.length == 13 || (t_no.length == 14 && t_no.indexOf("-") == 3)) {
                            go = true;
                        }
                        else {
                            Ext.MessageBox.alert('提示', "票号不符合规范");
                        }
                        if (go) {
                            ticketNoFormPanel.form.submit({
                                waitMsg: 'In processing',
                                success: function(form, action){
                                    Ext.MessageBox.alert('操作成功', "保存票号成功");
                                    ticketNoWindow.hide();
                                    dataStore.reload();
                                },
                                failure: function(form, action){
                                    Ext.MessageBox.alert('操作失败', "保存票号失败");
                                }
                            });
                        }
                    }
                }
            }, {
                text: '取消',
                handler: function(){
                    ticketNoWindow.hide();
                }
            }]
        });
        ticketNoWindow.show();
        
    }
})
